﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Diagnostics;

namespace Axe.ComponentModel
{

    public abstract class ModalFormEditor : UITypeEditor
    {
        protected abstract EditorForm GetEditorForm();

        /// <summary>
        /// Реализация метода редактирования
        /// </summary>
        public override Object EditValue(ITypeDescriptorContext context,IServiceProvider provider,Object value)
        {
            if ((context != null) && (provider != null))
            {
                IWindowsFormsEditorService svc =
                  (IWindowsFormsEditorService)
                  provider.GetService(typeof(IWindowsFormsEditorService));

                if (svc != null)
                {
                    try
                    {
                        using (EditorForm ipfrm = GetEditorForm())
                        {
                            ipfrm.SetEditedObject(value, context.Instance);

                            if (svc.ShowDialog(ipfrm) == DialogResult.OK)
                            {
                                value = ipfrm.GetEditedObject();
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine(e.Message);
                    }
                }
            }

            return base.EditValue(context, provider, value);
        }

        /// <summary>
        /// Возвращаем стиль редактора - модальное окно
        /// </summary>
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            if (context != null)
                return UITypeEditorEditStyle.Modal;
            else
                return base.GetEditStyle(context);
        }
    }

    public class EditorForm : Form, IObjectEditor
    {
        public virtual  void SetEditedObject(object value, object parentProperty)
        {
            throw new NotImplementedException();
        }

        public virtual object GetEditedObject()
        {
            throw new NotImplementedException();
        }
    }

}
